#include "fig_ui_popupmanager.h"
#include "fig_ui_settings.h"
#include "fig_ui_levelcomplete.h"
#include "fig_ui_yesno.h"
#include "fig_ui_pausemenu.h"
#include "fig_ui_medalinfo.h"
#include "fig_ui_popupcontroller.h"

#include "input/moga.h"
#include "audio/audio.h"
#include "ui/ui_manager.h"
#include "gameflow/game_flow_manager.h"
#include "gameflow/ui_popup.h"
#include "gameflow/ui_state.h"

#include "util/log.h"
#include "util/common_macros.h"

using namespace gameflow;

FIGUIPopupAnimator::FIGUIPopupAnimator(const std::string& Name,const std::string& CloseButton) :
									m_pCloseButton(NULL),
									m_pPopup(NULL),
									m_pAnimateWidget(NULL)
{
	m_pPopup = UIPopupManager::Instance().AddPopup(Name,CloseButton);
	if(!CloseButton.empty() && m_pPopup)
	{
		m_pCloseButton  = m_pPopup->GetWidget(CloseButton);		
	}
}

FIGUIPopupAnimator::FIGUIPopupAnimator(gameflow::UIPopup* pPopup) : 
								m_pPopup(pPopup),
								m_pAnimateWidget(NULL),
								m_pCloseButton(NULL)
{
	UIPopupManager::Instance().AddPopup(pPopup);	
}


FIGUIPopupAnimator::FIGUIPopupAnimator() :	m_pAnimateWidget(NULL),
											m_pCloseButton(NULL),											
											m_pPopup(NULL)
{

}

void	FIGUIPopupAnimator::SetCloseButton(const std::string& CloseButton)
{
	if(!CloseButton.empty() && m_pPopup)
	{
		m_pCloseButton  = m_pPopup->GetWidget(CloseButton);		
	}
}

FIGUIPopupAnimatorMgr::~FIGUIPopupAnimatorMgr()
{
	// :TODO 
	// if i enable this code ( which I should ) , windows will carsh at game exists
	//for(int i = 0; i < POPUP_NAMES_C; i++)
	//	UTIL_SAFE_DELETE(m_pPopups[i]);
}

FIGUIPopupAnimatorMgr&     FIGUIPopupAnimatorMgr::Instance()
{
	static FIGUIPopupAnimatorMgr	Inst;
	return Inst;
}

FIGUIPopupAnimatorMgr::FIGUIPopupAnimatorMgr()
{
	for(int i = 0; i < POPUP_NAMES_C; i++)
		m_pPopups[i] = NULL;
	
}

void		FIGUIPopupAnimatorMgr::AddPopupAnimator(POPUP_NAMES Name,FIGUIPopupAnimator* pPopupAnimator )
{
	if(pPopupAnimator)
		m_pPopups[Name] = pPopupAnimator;
}




void	FIGUIPopupAnimatorMgr::Tick(float dt)
{
	for(int i = 0; i < POPUP_NAMES_C; i++)
	{
		if(m_pPopups[i])
			m_pPopups[i]->Tick(dt);
	}
}

void	FIGUIPopupAnimatorMgr::OnMouseButtonDown(const math::Vec2& Pos)
{
	for(int i = 0; i < POPUP_NAMES_C; i++)
	{
		if(m_pPopups[i])
			m_pPopups[i]->OnMouseButtonDown(Pos);
	}
}

void	FIGUIPopupAnimator::Hide()
{
	if(m_pPopup)
		m_pPopup->Hide(NULL);
	AUDIO().PlayEvent("popup_close");
	
}

FIGUIPopupAnimator*	FIGUIPopupAnimatorMgr::Show(POPUP_NAMES Name)
{
	

	if(m_pPopups[Name])
	{
		m_pPopups[Name]->Show();
		return m_pPopups[Name];
	}
	else
	{
		

		if(GetPopup(Name))
		{
			m_pPopups[Name]->Show();
		}
	}	
	return m_pPopups[Name];

}

FIGUIPopupAnimator*		FIGUIPopupAnimatorMgr::GetPopup(POPUP_NAMES Name)
{
	float AspectRation = (float)( (float)GetGraphics()->GetScreenHeight()  / (float)GetGraphics()->GetScreenWidth()); 
	
	if(m_pPopups[Name])
		return m_pPopups[Name];

	// lazy loading
	switch(Name)
	{	
	case POPUP_NAMES_SETTING:
		{
			gameflow::UIPopup *pPopup = gameflow::UIPopupManager::Instance().GetPopup("ui/settings.ui");
			new FIGUISettings(pPopup);
		}
		break;
	case POPUP_NAMES_PAUSEMENU:
		FIGUIPauseMenu::Instance();
		break;
	case POPUP_NAMES_MSGBOX:
		//new MATUIMsgBox();
		break;
	case POPUP_NAMES_GAMEOVER:
		if(AspectRation >= 1.65f)
			new  FIGUILevelComplete("ui/5_level_complete.ui");
		else if(AspectRation <= 1.34 )
			new FIGUILevelComplete("ui/ipad_level_complete.ui");  	
		else  		
			new FIGUILevelComplete("ui/level_complete.ui");

		break;
	
	case POPUP_NAMES_YESNO:
		{
			gameflow::UIPopup* pPopup = gameflow::UIPopupManager::Instance().GetPopup("ui/yesno.ui");
			new FIGUIYesNo(pPopup);
		}
		break;
	case POPUP_NAMES_MEDALINFO:
		new FIGUIMedalInfo("ui/popup_medal_info.ui","button_ok");
		break;

	case POPUP_NAMES_CONTROLLER:
		new FIGUIPopupController();
		break;
	
	}

	return m_pPopups[Name];
}

void	FIGUIPopupAnimator::Show()
{
	AUDIO().PlayEvent("popup_open");

	if(m_pPopup)
		m_pPopup->Show(NULL);

	if(m_pCurrentSelectUI)
	{
		m_pCurrentSelectUI->SetState(ui::UIWidget::WS_MOUSE_OFF);

		if((int)m_UIs.size() > 0)
			m_pCurrentSelectUI = m_UIs[0];

		FIGUIPopupAnimator::OnMOGAUIEnter();
	}
	/*
	switch(m_Type)
	{	
	case ANIMATION_MSGPOPUP:
		//MATUIAnimator::Instance().Add(m_pAnimateWidget,MATAnimatedUI::ANIMATE_STYLE_POPPINGUP,0, NULL);
		break;
	case ANIMATION_SLIDEFROMRIGHT:
		if(m_pAnimateWidget)
			m_pAnimateWidget->SetPosition(math::Vec2(1.5f,0.5f));
		//MATUIAnimator::Instance().Add(m_pAnimateWidget,MATAnimatedUI::ANIMATE_STYLE_CENTERSCREEN,0, NULL);
		break;
	case ANIMATION_SLIDEFROMTOP:
		if(m_pAnimateWidget)
			m_pAnimateWidget->SetPosition(math::Vec2(0.5f,-1.5f));
		//MATUIAnimator::Instance().Add(m_pAnimateWidget,MATAnimatedUI::ANIMATE_STYLE_CENTERSCREEN,0, NULL);
		break;
	default:
		break;
	}
	*/

	ui::UIManager::Instance().Tick(0);
	
	
}

void	FIGUIPopupAnimator::Tick(float dt)
{
	

	switch(m_Type)
	{
	case ANIMATION_MSGPOPUP:

		break;
	case ANIMATION_SLIDEFROMRIGHT:

		break;
	case ANIMATION_SLIDEFROMTOP:


		break;
	default:
		break;
	}

}

bool	FIGUIPopupAnimatorMgr::KeyPressed(input::Keyboard::KEY* pKey)
{
	for(int i = 0; i < POPUP_NAMES_C; i++)
	{
		if(m_pPopups[i])
			if(m_pPopups[i]->KeyPressed(pKey))
				return true;
	}
	return false;
}

bool	FIGUIPopupAnimatorMgr::OnUIKeyDownEvent(int KeyCode)
{
	
	for(int i = 0; i < POPUP_NAMES_C; i++)
	{
		if(m_pPopups[i])
		{
			if(m_pPopups[i]->OnUIKeyDownEvent(KeyCode))
				return true;
		}
	}
	return false;
}

bool	FIGUIPopupAnimator::OnUIKeyDownEvent(int KeyCode)
{
	
	if(m_pPopup && m_pPopup->IsActive())
	{
		if(KeyCode == input::MOGAJoystick::KEYCODE_BUTTON_B)
		{
			if(m_pCloseButton)
				m_pCloseButton->OnMouseRelease(m_pCloseButton->GetPosition(), (int)input::Mouse::BUTTON_LEFT);
			this->Hide();
			return true;
		}
		else
		{
			FIGMOGANavigation::OnUIKeyDownEvent(KeyCode);
			return true;
		}
		
	}
	return false; 
}
